package com.coffee.admin.service.platform.impl;

import com.coffee.admin.api.platform.query.PlatformRoleRelationQuery;
import com.coffee.admin.api.platform.result.PlatformRoleRelationResult;
import com.coffee.admin.api.platform.service.PlatformRoleRelationService;
import com.coffee.admin.service.platform.bean.PlatformRoleRelation;
import com.coffee.admin.service.platform.dao.PlatformRoleRelationDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class PlatformRoleRelationServiceImpl implements PlatformRoleRelationService {
	@Resource
	private PlatformRoleRelationDao	platformRoleRelationDao;

	@Override
	public Page<PlatformRoleRelationResult> getPager(PlatformRoleRelationQuery param, PagerInfo pr) {
		Page<PlatformRoleRelationResult> pager=new Page<PlatformRoleRelationResult>(pr.getStart(), pr.getPageSize());
		Integer count=platformRoleRelationDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<PlatformRoleRelation> list=platformRoleRelationDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, PlatformRoleRelationResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(PlatformRoleRelationQuery param){
		Integer count=platformRoleRelationDao.count(param);
		return count;
	}
	
	@Override
	public PlatformRoleRelationResult getDetail(Integer id){
		PlatformRoleRelation item=platformRoleRelationDao.findById(id);
		if(item==null){
			return null;
		}
		PlatformRoleRelationResult result=new PlatformRoleRelationResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<PlatformRoleRelationResult> getList(PlatformRoleRelationQuery param, PagerInfo pr){
		List<PlatformRoleRelation> list=platformRoleRelationDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, PlatformRoleRelationResult.class);
	}
	
	@Override
	public List<PlatformRoleRelationResult> getList(PlatformRoleRelationQuery param){
		List<PlatformRoleRelation> list=platformRoleRelationDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, PlatformRoleRelationResult.class);
	}
	
	@Override
	public List<PlatformRoleRelationResult> getList(){
		PlatformRoleRelationQuery param = new PlatformRoleRelationQuery();
		List<PlatformRoleRelation> list=platformRoleRelationDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, PlatformRoleRelationResult.class);
	}


	@Override
	public void delete(Integer id) {
		platformRoleRelationDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(PlatformRoleRelationResult result)throws BusinessException {
		PlatformRoleRelation item=new PlatformRoleRelation();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			platformRoleRelationDao.insert(item);
		}else {
			PlatformRoleRelation tmp=platformRoleRelationDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			platformRoleRelationDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,PlatformRoleRelationResult> getItemMap(Set<Integer> idSet){
		Map<Integer, PlatformRoleRelationResult> map=new HashMap<Integer, PlatformRoleRelationResult>();
		List<PlatformRoleRelation> list=platformRoleRelationDao.findByIds(StringUtil.setToList(idSet));
		List<PlatformRoleRelationResult> resultList=DTOUtils.convertList(list, PlatformRoleRelationResult.class);
		for(PlatformRoleRelationResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,PlatformRoleRelationResult> getItemMap(){
		Map<Integer, PlatformRoleRelationResult> map=new HashMap<Integer, PlatformRoleRelationResult>();
		PlatformRoleRelationQuery param=new PlatformRoleRelationQuery();
		List<PlatformRoleRelation> list=platformRoleRelationDao.page(param, 0, Integer.MAX_VALUE);
		List<PlatformRoleRelationResult> resultList=DTOUtils.convertList(list, PlatformRoleRelationResult.class);
		for(PlatformRoleRelationResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
